package com.example.hasee.zhcd.Activities.ChuanWu;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Activities.ShangWu.AtPortActivity;
import com.example.hasee.zhcd.Base.BaseActivity;
import com.example.hasee.zhcd.Bean.AtWorkItemInfo;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class AtWorkActivity extends BaseActivity {
    private HashMap<String, String> loginmap = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private TextView title;
    private ImageView back;
    private ImageView home;
    private ListView at_work_list;
    private ArrayList<Boolean> bol_list = new ArrayList<Boolean>();
    public static ArrayList<AtWorkItemInfo> list = new ArrayList<>();
    private AtWorkAdapter adapter;

    private String strSYN;
    private String strGZNR;
    private String strWBSXCODE;
    private String strWBSXID;
    private String strDLR;
    private String strAccount;

    @Override
    public int contentViewId() {
        return R.layout.activity_at_work;
    }

    @Override
    public void set() {
        title.setText("当班工作情况");
        back.setOnClickListener(this);
        home.setOnClickListener(this);
        home.setImageResource(R.mipmap.edit);
        getAtWorkList();
    }

    @Override
    public void init() {
        strAccount = loginmap.get("account");
        strDLR = loginmap.get("user");
        //{"user":"许洪霞", "role":"", "account":"805", "user":"5530"}
//        strAccount = "805";
//        strDLR = "5530";
        strSYN = "";
        strGZNR = "";
        strWBSXCODE = "";
        strWBSXID = "";
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        at_work_list = (ListView) findViewById(R.id.at_work_list);
    }

    private void getAtWorkList() {
        ProgressDialogUtils.createProgressDialog(AtWorkActivity.this, "正在获取在港船列表...");
        HashMap<String, String> map = new HashMap<>();
        httpUtils.get("ZHCW_WorkList", map, "ZHCW_WorkList");
    }

    private void save_wqlr() {
        ProgressDialogUtils.createProgressDialog(AtWorkActivity.this, "正在保存...");
        HashMap<String, String> map = new HashMap<>();
        map.put("strSYN", strSYN);
        map.put("strGZNR", strGZNR);
        map.put("strWBSXCODE", strWBSXCODE);
        map.put("strWBSXID", strWBSXID);
        map.put("strDLR", strDLR);
        map.put("strAccount", strAccount);
        httpUtils.get("ZHCW_WQLR", map, "ZHCW_WQLR");
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZHCW_WorkList":
                ProgressDialogUtils.closeProgressDialog();
                Log.e("ZHCW_WorkList", msg.getMsg());
                list.clear();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        for (int i = 0; i < data.length(); i++) {
                            AtWorkItemInfo info = new AtWorkItemInfo();
                            info.setSelected(false);
                            info.setFsyn(data.getJSONObject(i).getString("fsyn"));
                            Log.e("接口取出的", info.getFsyn());
                            info.setFberth(data.getJSONObject(i).getString("fberth"));
                            info.setFshipname(data.getJSONObject(i).getString("fshipname"));
//                            info.setFhc(data.getJSONObject(i).getString("fhc"));
                            info.setFwbsxcode(data.getJSONObject(i).getString("fwbsxcode"));
                            info.setFwbsx(data.getJSONObject(i).getString("fwbsx"));
                            info.setFtzrn(data.getJSONObject(i).getString("ftzrn"));
                            info.setFwbsxid(data.getJSONObject(i).getString("fwbsxid"));
                            info.setFkbsj(data.getJSONObject(i).getString("fkbsj"));
                            list.add(info);
                        }
                        adapter = new AtWorkAdapter();
                        at_work_list.setAdapter(adapter);
                    } else {
                        Toast.makeText(AtWorkActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            case "ZHCW_WQLR":
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    Toast.makeText(AtWorkActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                getAtWorkList();
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.home:
                strSYN = "";
                strGZNR = "";
                strWBSXCODE = "";
                strWBSXID = "";
                bol_list.clear();
                for (int j = 0; j < list.size(); j++) {
                    bol_list.add(list.get(j).isSelected());
                }
                if (bol_list.contains(true)) {
                    for (int k = 0; k < bol_list.size(); k++) {
                        if (bol_list.get(k) == true) {
                            if ("".equals(strSYN)) {
                                strSYN = list.get(k).getFsyn();
                                strGZNR = list.get(k).getFtzrn();
                                strWBSXCODE = list.get(k).getFwbsxcode();
                                strWBSXID = list.get(k).getFwbsxid();
                            } else {
                                strSYN = strSYN + "," + list.get(k).getFsyn();
                                strGZNR = strGZNR + "," + list.get(k).getFtzrn();
                                strWBSXCODE = strWBSXCODE + "," + list.get(k).getFwbsxcode();
                                strWBSXID = strWBSXID + "," + list.get(k).getFwbsxid();
                            }
                        }
                    }
                    save_wqlr();
  //                  Log.e("参数",strSYN+"   "+strGZNR+"   "+strWBSXCODE+"   "+strWBSXID+"   "+strDLR+"   "+strAccount);
                } else {
                    Toast.makeText(AtWorkActivity.this, "请选择需要上传的当班工作情况！", Toast.LENGTH_LONG).show();
                }
                break;
        }
    }

    class AtWorkAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return null != list ? list.get(i) : null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            Holder holder = null;
            AtWorkItemInfo info = (AtWorkItemInfo) getItem(i);


            if (null == holder) {
                LayoutInflater inflater = (LayoutInflater) AtWorkActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.at_work_list_item, null);
                holder = new Holder();

                holder.checkBox = (CheckBox) view.findViewById(R.id.flr);
                holder.fberth = (TextView) view.findViewById(R.id.fberth);
                holder.fshipname = (TextView) view.findViewById(R.id.fshipname);
                holder.fkbsj = (TextView) view.findViewById(R.id.fkbsj);
                holder.fwbsx = (TextView) view.findViewById(R.id.fwbsx);
                holder.ftzrn = (TextView) view.findViewById(R.id.ftzrn);
                final Holder fholder = holder;
                holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        AtWorkItemInfo info = (AtWorkItemInfo) fholder.checkBox.getTag();
                        info.setSelected(buttonView.isChecked());
                    }
                });
                view.setTag(holder);
                holder.checkBox.setTag(info);
            } else {
                holder = (Holder) view.getTag();
                holder.checkBox.setTag(info);
            }
            holder.checkBox.setChecked(info.isSelected());
            holder.fberth.setText(info.getFberth());
            holder.fshipname.setText(info.getFshipname());
            holder.fkbsj.setText(info.getFkbsj());
            holder.fwbsx.setText(info.getFwbsx());
            holder.ftzrn.setText(info.getFtzrn());
            return view;
        }

        class Holder {
            private CheckBox checkBox;
            private TextView fberth;
            private TextView fshipname;
            private TextView fkbsj;
            private TextView fwbsx;
            private TextView ftzrn;
        }
    }
}
